package org.domon.manager.util;

import org.apache.ibatis.session.RowBounds;

public class PageUtil2 {
	
	public final static String PAGE_STRING = "pageString";
	public final static String CURRENT_PAGE = "currentPage";
	public final static String DEFAULT_FORMNAME = "pageform"; 
	
	/**
	 * 得到RowBounds对象 方便分页查询
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	private static RowBounds get(int pageNo, int pageSize) {
		int first = getFirst(pageNo, pageSize);
		int last = getLast(first, pageSize);
		return new RowBounds(first, last);
	}
	
	private static int getFirst(int pageNo, int pageSize) {
		return (pageNo - 1) * pageSize;
	}

	private static int getLast(int first, int pageSize) {
		return first + pageSize;
	}
	
	/**
	 * 通过总行数和每页页数得到总页数
	 * @param totalPage
	 * @param perPage
	 * @return
	 */
	private static int getTotalPage(int totalPage,int perPage){
		if(perPage!=0){
			if(totalPage%perPage!=0){
				return totalPage/perPage+1;
			}else{
				return totalPage/perPage;
			}
			
		}else{
			return 0;
		}
	}
	
	/**
	 * 验证请求过来的页面数是否符合要求
	 * @param requestPage
	 * @return
	 */
	private static int validateRequestPage(String requestPage){
		int currentPage = 1;
		if(requestPage!=null && !requestPage.equals("")){
			try{
				currentPage = Integer.parseInt(requestPage);
			}catch(Exception e){
				e.printStackTrace();
				currentPage = 1;
			}
		}
		return currentPage;
	}
	
	/**
	 * 验证当前页数是否在该对象范围之内
	 * @param currentPage
	 * @param totalPage
	 * @return
	 */
	private static int validateTotalPage(int currentPage,int totalPage){
		if(currentPage>totalPage){
			currentPage = totalPage;
		}
		if(currentPage<1){
			currentPage = 1;
		}
		
		return currentPage;
	}
	
	private static String getPageString(int currentPage,int totalPage,int totalRow,String url,String formName){
		
		StringBuffer sb = new StringBuffer();
		
		sb.append("<script type=\"text/javascript\">");
        sb.append("function pageSubmit(page){");
        sb.append("resetPage(page);");
        sb.append("var url = \""+url+"?currentPage=\"+page;");
        sb.append("document."+formName+".action = url;");
        sb.append("document."+formName+".submit();");
        sb.append("}");
        sb.append("</script>");
        
		sb.append("<div id=\"pageMessage\">");
		
        if(currentPage==1){
        	sb.append("<a href=\"#\">首页</a>&nbsp;&nbsp;");
        }
        if(currentPage>1){
        	sb.append("<a href=\"#\" onclick=\"pageSubmit(1)\">首页</a>&nbsp;&nbsp;");
        }
        
        sb.append("<a href=\"#\" onclick=\"pageSubmit("+(currentPage-1)+")\">上一页</a>&nbsp;&nbsp;");
        sb.append(""+currentPage+"&nbsp;&nbsp;");
		
        sb.append("<a href=\"#\" onclick=\"pageSubmit("+(currentPage+1)+")\">下一页</a>&nbsp;&nbsp;");
		
        if(currentPage==totalPage || totalPage == 0){
        	 sb.append("<a href=\"#\">尾页</a>&nbsp;&nbsp;");
        }
        if(currentPage<totalPage){
        	 sb.append("<a href=\"#\" onclick=\"pageSubmit("+totalPage+")\">尾页</a>&nbsp;&nbsp;");
        }
        
        sb.append("总共<font id=\"myTotal\" color=\"blue\">"+totalPage+"</font>页&nbsp;&nbsp;<font  color=\"blue\">"+totalRow+"</font>条记录");
        sb.append("转到第<input id=\"currentPage\" name=\"currentPage\" type=\"text\" size=\"1\" value="+currentPage+"></input>页");
        sb.append("</div>");
        
		return sb.toString();
	}
	
	private static String getPageString2(int currentPage,int totalPage,int totalRow,String contextPath,String url,String formName){
		
		String res = contextPath+"/resources";
		
		StringBuffer sb = new StringBuffer();
		
		sb.append("<script type=\"text/javascript\">");
        sb.append("function pageSubmit(page){");
        sb.append("resetPage(page);");
        sb.append("var url = \""+url+"?currentPage=\"+page;");
        sb.append("document."+formName+".action = url;");
        sb.append("document."+formName+".submit();");
        sb.append("}");
        sb.append("</script>");
        
		sb.append("<div class=\"page\" id=\"pageMessage\">");
		sb.append("<table width=\"500\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\" style=\"margin-top:8px;\" > ");
		sb.append("<tr>");
		
		if(currentPage==1){
			sb.append("<td width=\"50\" align=\"center\"><a href=\"#\" ><img src=\""+res+"/images/first.gif\" width=\"42\" height=\"22\" border=\"0\" align=\"absmiddle\"/></a></td>  ");
		}
		if(currentPage>1){
			sb.append("<td width=\"50\" align=\"center\"><a href=\"#\" onclick=\"pageSubmit(1);\"><img src=\""+res+"/images/first.gif\" width=\"42\" height=\"22\" border=\"0\" align=\"absmiddle\"/></a></td>  ");
		}
		
		sb.append("<td width=\"50\" align=\"center\"><a href=\"#\" onclick=\"pageSubmit("+(currentPage-1)+");\"><img src=\""+res+"/images/pre.gif\"  width=\"42\" height=\"22\" border=\"0\" align=\"absmiddle\"/></a></td>  ");
		sb.append("<td width=\"50\" align=\"center\"><a href=\"#\" onclick=\"pageSubmit("+(currentPage+1)+");\"><img src=\""+res+"/images/next1.gif\" width=\"42\" height=\"22\" border=\"0\" align=\"absmiddle\"/></a></td>  ");
		
		if(currentPage == totalPage || totalPage == 0){
			sb.append("<td width=\"50\" align=\"center\"><a href=\"#\" ><img src=\""+res+"/images/Last.gif\" width=\"42\" height=\"22\" border=\"0\" align=\"absmiddle\"/></a></td>  ");
		}
		if(currentPage<totalPage){
			sb.append("<td width=\"50\" align=\"center\"><a href=\"#\" onclick=\"pageSubmit("+totalPage+");\"><img src=\""+res+"/images/Last.gif\" width=\"42\" height=\"22\" border=\"0\" align=\"absmiddle\"/></a></td>  ");
		}
		
		sb.append("<td width=\"200\" align=\"right\">总共<span id=\"myTotal\" style=\"color:#FF6600;\">"+totalPage+"</span>页"+totalRow+"条记录 </td>  ");
		sb.append("<td width=\"100\" align=\"right\"> 转到第<label><input type=\"text\" name=\"currentPage\" id=\"currentPage\" style=\"width:20px;\" value="+currentPage+" />页</label> </td>");
		
		sb.append("</tr>");
		sb.append("</table>");
		sb.append("</div>");
        
		return sb.toString();
	}
	
	private static String getPageString(int currentPage,int totalPage,int totalRow,String contextPath,String url,String formName){
		
		String res = contextPath+"/resources";
		
		StringBuffer sb = new StringBuffer();
		
		sb.append("<script type=\"text/javascript\">");
        sb.append("function pageSubmit(page){");
        sb.append("resetPage(page);");
        sb.append("var url = \""+url+"?currentPage=\"+page;");
        sb.append("document."+formName+".action = url;");
        sb.append("document."+formName+".submit();");
        sb.append("}");
        sb.append("</script>");
        
        sb.append("<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" class=\"madding2\">");
        sb.append("<tr>");
        sb.append("<td width=\"2%\" align=\"left\"><img src=\""+res+"/images/new/jgico_03.gif\" width=\"14\" height=\"14\" /></td>");
        sb.append("<td width=\"80%\" align=\"left\" style=\"font-size:12px;\">查询结果：共 <span style=\" color:#FF3300\">"+totalRow+"</span> 条记录 当前 <span style=\"color:#ff3300;\">"+currentPage+"/"+totalPage+" </span>页</td>");
        sb.append("<td align=\"right\">");
        
        if(currentPage==1){
        	sb.append("<img src=\""+res+"/images/new/pre.gif\" width=\"16\" height=\"19\" />&nbsp;");
        }
        if(currentPage>1){
        	sb.append("<img src=\""+res+"/images/new/pre.gif\" width=\"16\" height=\"19\"  onclick=\"pageSubmit(1)\"/>&nbsp;");
        }
        sb.append("<img src=\""+res+"/images/new/pre1.gif\" width=\"55\" height=\"19\"  onclick=\"pageSubmit("+(currentPage-1)+")\"/>&nbsp;");
        sb.append("<img src=\""+res+"/images/new/next1.gif\" width=\"55\" height=\"19\"  onclick=\"pageSubmit("+(currentPage+1)+")\"/>&nbsp;");
        
        if(currentPage==totalPage || totalPage == 0){
        	sb.append("<img src=\""+res+"/images/new/next.gif\" width=\"16\" height=\"19\" /></td>");
        }
        if(currentPage<totalPage){
        	sb.append("<img src=\""+res+"/images/new/next.gif\" width=\"16\" height=\"19\"  onclick=\"pageSubmit("+totalPage+")\"/></td>");
        }
        
        sb.append("</tr>");
        sb.append("</table>");
        sb.append("<div style='display:none'><input id=\"currentPage\" name=\"currentPage\" type=\"text\" size=\"1\" value="+currentPage+"></input></div>");
        
		return sb.toString();
	}
	
	/**
	 * 页面form名称与DEFAULT_FORMNAME相同
	 * @param myCurrentPage 请求过来当前的页数
	 * @param totalRow  查询出来总共多少行
	 * @param perPage   每页多少行
	 * @param url  页面跳转请求路径
	 * @param formName  表单名称
	 * @return 
	 */
	public static PageObject makePageString(String myCurrentPage,int totalRow,int perPage,String contextPath,String url,String formName){
		//验证下请求过来的当前页数是否符合要求
		int currentPage = validateRequestPage(myCurrentPage);
		//通过总行数和每页页数得到总页数
		int totalPage = PageUtil2.getTotalPage(totalRow,perPage);
		//验证当前页数是否在总页数区间范围内
		currentPage = PageUtil2.validateTotalPage(currentPage, totalPage);
		//生成页面打印字符串
		String pageString = PageUtil2.getPageString2(currentPage, totalPage,totalRow,contextPath,url,formName);
		//String pageString = PageUtil2.getPageString2(currentPage, totalPage, totalRow, url, formName);
		
		RowBounds rb = get(currentPage,perPage);
		
		PageObject pageObject = new PageObject();
		pageObject.setPageString(pageString);
		pageObject.setRowBounds(rb);
		pageObject.setTotalRow(totalRow);
		
		pageObject.setTotalPage(totalPage);
		
		System.out.println(pageString);
		
		return pageObject;
	}

	
	/**
	 * 不经过打印字符串
	 * @param myCurrentPage
	 * @param totalRow
	 * @param perPage
	 * @return
	 */
	public static PageObject makePageObject(String myCurrentPage,int totalRow,int perPage){
		//验证下请求过来的当前页数是否符合要求
		int currentPage = validateRequestPage(myCurrentPage);
		//通过总行数和每页页数得到总页数
		int totalPage = PageUtil2.getTotalPage(totalRow,perPage);
		//验证当前页数是否在总页数区间范围内
		currentPage = PageUtil2.validateTotalPage(currentPage, totalPage);
		
		RowBounds rb = get(currentPage,perPage);
		
		PageObject pageObject = new PageObject();
		pageObject.setRowBounds(rb);
		pageObject.setCurrentPage(currentPage);
		pageObject.setTotalPage(totalPage);
		pageObject.setPerPage(perPage);
		pageObject.setTotalRow(totalRow);
		
		return pageObject;
	}
}
